package com.news.service.impl;


import com.github.pagehelper.PageHelper;

import com.news.mapper.NewsMapper;
import com.news.model.News;
import com.news.model.NewsExample;
import com.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;
    public void insertNews(News news) {
       int i =  newsMapper.insert(news);
       if(i!=1) {
           throw new RuntimeException("插入数据出错");
       }
    }

    public void uqdateNews(News news) {
        newsMapper.updateByPrimaryKey(news);
    }

    public List<News> queryAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        NewsExample ne = new NewsExample();
        ne.setOrderByClause("id");
        return newsMapper.selectByExample(ne);
    }

    @Override
    public List<News> queryNews(int pageSize, int startRecord) {
        List<News> news = newsMapper.queryAll(pageSize, pageSize + startRecord);
        return news;
    }

    public List<News> queryCategory(String cateGoryID) {
        NewsExample ne = new NewsExample();
        NewsExample.Criteria criteria = ne.createCriteria();
        criteria.andCategoryIdEqualTo(cateGoryID);
        return newsMapper.selectByExample(ne);
    }

    @Override
    public News queryOne(String id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int queryCount() {
        return newsMapper.queryCount();
    }

    @Override
    public List<News> queryOneNews(String name) {
        NewsExample ne = new NewsExample();
        ne.or().andTitleLike("%"+name+"%");
        return newsMapper.selectByExample(ne);
    }

    @Override
    public List<News> queryOneNews1(String name,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        NewsExample ne = new NewsExample();
        ne.or().andTitleLike("%"+name+"%");
        return newsMapper.selectByExample(ne);
    }

    @Override
    public List<News> queryCategory1(String cateGoryID,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        NewsExample ne = new NewsExample();
        NewsExample.Criteria criteria = ne.createCriteria();
        criteria.andCategoryIdEqualTo(cateGoryID);
        return newsMapper.selectByExample(ne);
    }


}
